package org.bmi.gwt.examples.array.client;

import org.bmi.gwt.examples.array.shared.Component;
import org.bmi.gwt.examples.array.shared.ObjArrayProxy;
import org.bmi.gwt.examples.array.shared.PrinterObject;
import org.bmi.gwt.examples.array.shared.PrinterProxy;
import org.bmi.gwt.examples.array.shared.StringArrayProxy;
import org.bmi.gwt.examples.array.shared.intArrayProxy;
import org.bmi.gwt.mi.client.channel.Connection;
import org.bmi.gwt.mi.client.events.ConnectionHandler;
import org.bmi.gwt.mi.shared.BroadChannel;
import org.bmi.gwt.mi.shared.Channel;
import org.bmi.gwt.mi.shared.MiTransaction;
import org.bmi.gwt.mi.shared.callbacks.BindCallback;
import org.bmi.gwt.mi.shared.communication.var.ReturnVar;
import org.bmi.gwt.mi.shared.exceptions.InvalidArgumentException;
import org.bmi.gwt.mi.shared.exceptions.MiException;
import org.bmi.gwt.mi.shared.exceptions.NameParseException;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;

public class Array implements EntryPoint
{
	@Override
	public void onModuleLoad()
	{
		Connection.createConnection(new ConnectionHandler()
		{
			@Override
			public void onException(Exception e)
			{
				GWT.log("an error occurred: ", e);
			}

			@Override
			public void onConnectionBegin(Connection connection)
			{
				try
				{
					Channel qListen = connection.getChannel("org.bmi.gwt.mi.test.array.client");
					qListen.register("client_printer_id", new PrinterObject()
					{
						@Override
						public void print(String str)
						{
							RootPanel rootPanel = RootPanel.get();
							Element body = rootPanel.getElement();

							body.appendChild(Document.get().createTextNode(str));
						}

						@Override
						public void printLn(String str)
						{
							RootPanel rootPanel = RootPanel.get();
							Element body = rootPanel.getElement();

							body.appendChild(Document.get().createTextNode(str));
							body.appendChild(Document.get().createBRElement());
						}
					});

					BroadChannel qSend = connection.getChannel("org.bmi.gwt.mi.test.array.server");

					/* A transaction is created... */
					final MiTransaction t = qSend.createTransaction();

					qListen.bind(new BindCallback()
					{
						@Override
						public void onError(MiException error)
						{
							GWT.log("An error occurred: " + error);
						}

						@Override
						public void onBound(Channel channel)
						{
							try
							{
								PrinterProxy printerPrx = t.proxyToObject(PrinterProxy.class, "server_printer_id");

								String[] strArray = new String[] { "aaa", "bbb", "ccc" };
								int[] intArray = new int[] { 11, 22, 33 };
								Component c1 = new Component();
								c1.setStr("c1");
								Component c2 = new Component();
								c2.setStr("c2");
								Component c3 = new Component();
								c3.setStr("c3");
								Component[] objArray = new Component[] { c1, c2, c3 };

								StringArrayProxy proxy1 = t.proxyToNewObject(StringArrayProxy.class, strArray,
										printerPrx);
								intArrayProxy proxy2 = t.proxyToNewObject(intArrayProxy.class, intArray, printerPrx);
								ObjArrayProxy proxy3 = t.proxyToNewObject(ObjArrayProxy.class, objArray, printerPrx);

								proxy1.print1Array(strArray);
								proxy2.print1Array(intArray);
								proxy3.print1Array(objArray);

								proxy1.print2Array(new String[][] { strArray, strArray, strArray });
								proxy2.print2Array(new int[][] { intArray, intArray, intArray });
								proxy3.print2Array(new Component[][] { objArray, objArray, objArray });

								printerPrx.printLn("and with return values...");
								ReturnVar<String[][]> a1 = proxy1.createArray();
								ReturnVar<int[][]> a2 = proxy2.createArray();
								ReturnVar<Component[][]> a3 = proxy3.createArray();

								proxy1.print2Array(a1);
								proxy2.print2Array(a2);
								proxy3.print2Array(a3);
							}
							catch (InvalidArgumentException e)
							{
								e.printStackTrace();
							}

							t.commit();
						}
					});
				}
				catch (NameParseException e)
				{
					e.printStackTrace();
				}
			}
		});
	}

}
